package com.airport.ticket.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.airport.ticket.common.DateUtil;
import com.airport.ticket.common.UrlUtil;
import com.airport.ticket.dao.FlightInfoMapper;
import com.airport.ticket.dao.OrderInfoMapper;
import com.airport.ticket.dao.UserInfoMapper;
import com.airport.ticket.entity.FlightInfo;
import com.airport.ticket.entity.FlightInfoExample;
import com.airport.ticket.entity.FlightInfoExample.Criteria;
import com.airport.ticket.entity.OrderInfo;
import com.airport.ticket.entity.UserInfo;
import com.airport.ticket.entity.UserInfoExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/admin")
public class OrderController {

	@Autowired
	private FlightInfoMapper flightInfoMapper;

	@Autowired
	private OrderInfoMapper orderInfoMapper;

	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Autowired
	private HttpServletRequest request;
	
	@RequestMapping(value = "/order_list.html")
    public String orderList(ModelMap map, String keyword, String departurePlace, String arrivalPlace, String departureDate, String pageNow, String pageSize, String errorMsg){
		map.addAttribute("nav", "3");
		map.addAttribute("title", "订单管理");
		map.addAttribute("keyword", keyword);
		map.addAttribute("departurePlace", departurePlace);
		map.addAttribute("arrivalPlace", arrivalPlace);
		map.addAttribute("departureDate", departureDate);
		
		if(errorMsg!=null && !"".equals(errorMsg)) {
			map.addAttribute("errorMsg", errorMsg);
		}
		
		if(pageNow==null || "".equals(pageNow) || Integer.parseInt(pageNow) < 1) {
			pageNow = "1";
		}
		
		if(pageSize==null || "".equals(pageSize) || Integer.parseInt(pageSize) < 1) {
			pageSize = "20";
		}
		
		OrderInfo condition = new OrderInfo();
		if(keyword!=null && !"".equals(keyword)) {
			condition.setFlightNumber(keyword);
		}
		if(departurePlace!=null && !"".equals(departurePlace)) {
			condition.setDeparturePlace(departurePlace);
		}
		if(arrivalPlace!=null && !"".equals(arrivalPlace)) {
			condition.setArrivalPlace(arrivalPlace);
		}
		if(departureDate!=null && !"".equals(departureDate)) {
			condition.setBeginDepartureDate(DateUtil.parseToDate(departureDate+" 00:00:00"));
			condition.setEndDepartureDate(DateUtil.parseToDate(departureDate+" 23:59:59"));
		}
		
		/**
		 * 查当前登录用户
		 */
		HttpSession session = request.getSession();
		UserInfo loginUserInfo = (UserInfo)session.getAttribute("loginUserInfo");
		
		/**
		 * 如果不是管理员，则只展示自己的订单
		 */
		if(!"1".equals(loginUserInfo.getUserType())) {
			condition.setUserId(loginUserInfo.getId());
		}
		
		
		PageHelper.startPage(Integer.parseInt(pageNow),Integer.parseInt(pageSize));
		List<OrderInfo> resultList = orderInfoMapper.selectOrderList(condition);
		
		if(resultList==null || resultList.size()==0){
			map.addAttribute("retCode", "99");
			map.addAttribute("pageNow", pageNow);
			map.addAttribute("pageSize", pageSize);
			map.addAttribute("totalPage", "0");
			return "admin/order/order_list";
        }
		
		PageInfo<OrderInfo> pageInfo = new PageInfo<>(resultList);
		map.addAttribute("pageNow", pageNow);
		map.addAttribute("pageSize", pageSize);
		map.addAttribute("totalPage", pageInfo.getPages());
		map.addAttribute("resultList", resultList);
			
		return "admin/order/order_list";
    }
	
	@RequestMapping(value = "/cancel_order.html")
    public String cancelOrder(ModelMap map, OrderInfo orderInfo){
		map.addAttribute("nav", "3");
		map.addAttribute("title", "取消机票");
		
		/**
		 * 判断订单是否存在
		 */
		OrderInfo order = orderInfoMapper.selectByPrimaryKey(orderInfo.getId());
		if(order==null || order.getId()==null || "".equals(order.getId())) {
			return "redirect:/admin/order_list.html?errorMsg="+UrlUtil.parseUtf8("取消失败，订单不存在");
		}
		
		if(!"1".equals(order.getStatus())) {
			return "redirect:/admin/order_list.html?errorMsg="+UrlUtil.parseUtf8("取消失败，订单未支付或已取消");
		}
		
		/**
		 * 判断航班是否存在
		 */
		FlightInfo flightInfo = flightInfoMapper.selectByPrimaryKey(order.getFlightId());
		if(flightInfo==null || flightInfo.getId()==null || "".equals(flightInfo.getId())) {
			return "redirect:/admin/order_list.html?errorMsg="+UrlUtil.parseUtf8("取消失败，航班不存在");
		}
		
		HttpSession session = request.getSession();
		UserInfo loginUserInfo = (UserInfo)session.getAttribute("loginUserInfo");
		
		/**
		 * 查当前登录用户
		 */
		UserInfo thisUser = userInfoMapper.selectByPrimaryKey(loginUserInfo.getId());
		
		/**
		 * 如果不是管理员，则只能取消自己的单
		 */
		if(!"1".equals(loginUserInfo.getUserType())) {
			/**
			 * 判断是否用户自己下的单
			 */
			if(!thisUser.getId().equals(order.getUserId())) {
				return "redirect:/admin/order_list.html?errorMsg="+UrlUtil.parseUtf8("取消失败，只能取消自己的订单");
			}
		}
		
		/**
		 * 设置订单状态为已取消
		 */
		order.setStatus("2");
		orderInfoMapper.updateByPrimaryKey(order);
		
		
		/**
		 * 取消完成，把用户的钱加上
		 */
		thisUser.setAmount(thisUser.getAmount().add(flightInfo.getPrice()));
		userInfoMapper.updateByPrimaryKey(thisUser);
		
		/**
		 * 更新session
		 */
		session.setAttribute("loginUserInfo",thisUser);
		
		return "redirect:/admin/order_list.html?errorMsg="+UrlUtil.parseUtf8("取消成功，款项已退回到余额，请查收");
    }
	
}
